package cn.zhaoguang.usedcar.common.util;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

/**
 * @Description 加密解密工具
 * @Author zhaoguang
 * @Date 2021/3/19 22:41
 */
public class RsaEncryptUtil {

    //公钥
    private static String PublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRcikCBuns3FG0mQQ9KfanUpJ9tFBBBXibv5rPa2NgSKs+7Ck3byWMP0rKt9VbIJjI1tvWOo2V9RUFpczDYA7eLgW9+kowVmJY9rcG7A2UPaTZBZBg+J3hOj9Z7obZgMrx3SVD9ykPH4A8uGdGjn6qvSW6sas8ljbw+snAKJvO8QIDAQAB";
    //私钥
    private static String PrivateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJFyKQIG6ezcUbSZBD0p9qdSkn20UEEFeJu/ms9rY2BIqz7sKTdvJYw/Ssq31VsgmMjW29Y6jZX1FQWlzMNgDt4uBb36SjBWYlj2twbsDZQ9pNkFkGD4neE6P1nuhtmAyvHdJUP3KQ8fgDy4Z0aOfqq9JbqxqzyWNvD6ycAom87xAgMBAAECgYBGmISzCSwN3bqC0CqwjWe9O2UDQrwZc1BhOJUpzFQF7f/AoNw4neg5tJbW4nfarW27Ph6iUvVaLgUwIpPK2Rsdm0whP56Uv43aNY4bfwE1SLup7wOQg8qHp08hVLC3C8ciuQ88CoUTlD2OreKOTIeRWiOYgc3jDSC4IDd+jOjKgQJBAN33XjSlpG25WQxTu4aH0C2B7g+14UXpj3XARNC/dxZMu3YlgWqqf2rEpw6XeurjKXJLUPLoP91cC53ZwPnOmYkCQQCnvzYy/1ayH6A7cU4n84kKguwpffw8ElaMmzZfB6f6IN7K55NXrQMqPMKfa40ApBGfq0/uxYX+NnQbal1/5XgpAkAKZrAMaYKl/Zx1nmrYJ/8SUtdT9yLiQsqNKwBOg9pRujDCnYlN9UVZPHPTlDeXUCO+Dldxun5boheNQwnu6sZ5AkBSyCQhKq1IHh9e9l2Rjbe1fkK5fzj5FDuuC/LbFtYotAv+qiWmgPUs5OtO7S6agF1KJKq/tK6Zc5n85ZLayQ1xAkB27MasLs2nqiuk0cnngg6Q5NmetiK6fhXUNDTaWOeant56yUqsmbYBlTJqxgZIYAFtE/pb9JknRJUrLYAUmV0d";

    /**
     * @Description RSA公钥加密
     * @Author zhaoguang
     */
    public static String publicKeyEncrypt(String data) {
        RSA rsa = new RSA(null, RsaEncryptUtil.PublicKey);
        String str = rsa.encryptBase64(data, KeyType.PublicKey);
        return str;

    }

    /**
     * @Description RSA私钥解密
     * @Author zhaoguang
     */
    public static String privateKeyDecrypt(String data) {
        RSA rsa = new RSA(RsaEncryptUtil.PrivateKey, null);
        String str = rsa.decryptStr(data, KeyType.PrivateKey);
        return str;
    }


//    public static void main(String[] args) {
//        System.out.println("------------------------Main方法测试------------------------");
//
//        RSA rsa = new RSA();
//
//        //获得公钥
//        String PublicKey = rsa.getPublicKeyBase64();
//        System.out.println("公钥：" + PublicKey);
//
//        //获得私钥
//        String PrivateKey = rsa.getPrivateKeyBase64();
//        System.out.println("私钥：" + PrivateKey);
//
//
//        String str = RsaEncryptUtil.publicKeyEncrypt("我是zhaoguang");
//        System.out.println("加密后的数据：" + str);
//
//        String x = RsaEncryptUtil.privateKeyDecrypt(str);
//        System.out.println("解密后的数据：" + x);
//    }

}
